package Commands;

import javax.naming.*;

import org.json.simple.*;

import Helpers.ApplicationLogger;

public final class ClientCommands {
	
	 private static final class Nested
     {
         static final ClientCommands _instance = new ClientCommands();
     }
	 
	 private ClientCommands()
	 {
		 
	 }

     public static ClientCommands getInstance()
     {
           return Nested._instance;
     }
     
     public ClientCommandResult ExecuteCommand(String commandName, String serializedCommand, ClientCommandContext context)
     {
    	Context ctx = null;
		try {
			ctx = new InitialContext();
		} catch (NamingException e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage());
		}
		
        IClientCommandExecuter command = null;
        try {
			command = (IClientCommandExecuter)ctx.lookup(commandName);
		} catch (NamingException e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage());
		}
			
	    ClientCommandResult result = new ClientCommandResult(); 
	    JSONObject jObj = (JSONObject) JSONValue.parse(serializedCommand); 
	    ApplicationLogger.getInstance().LOG.info("Command parsed");
	    
	    command.setContext(context);
        command.FillFromJSON(jObj);
        command.Execute();

        result = command.GetResult();

        return result;
     }
}
